{include file="common/head"}
<div class="card">
    <div class="card-header">
        <!-- <div class="row">
            <div class="col-md-4">
                <div class="row">
                    <label class="col-sm-4 col-form-label"><span class="text-danger">*</span> 角色搜索</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control pull-left" name="name" value="" placeholder="请输入角色名称">
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <button type="submit" class="btn btn-primary me-1">搜索</button>
                <button type="reset" class="btn btn-default">重置</button>
            </div>
        </div> -->
        <span class="text-primary">修改角色</span>
    </div>
    <div class="card-body">
        <form action="" class="layui-form" lay-filter="update">
            <div class="layui-form-item">
                <label class="col-sm-4 col-form-label"><span class="text-primary">* 角色名称</span></label>
                <div class="col-sm-8">
                    <input type="text" class="form-control pull-left" name="name" value="{$data.name}"
                        placeholder="请输入角色名称">
                </div>
            </div>
            <label class="col-sm-4 col-form-label"><span class="text-primary">* 操作方法</span></label>
            <div class="layui-form-item">
                {volist name="authList" id="auth"}
                <div class="parent">
                    <p class="text-success mb-3">【 {$auth.description} 】</p>
                    <input type="checkbox" name="auth[{$auth.id}]" title="{$auth.description}" id="{$auth.id}" >
                </div>
                {if $auth.child}
                {foreach $auth.child as $auths}
                <input type="checkbox" name="auth[{$auths.id}]" title="{$auths.description}" id="{$auths.id}">
                {/foreach}
                {/if}
                {/volist}
            </div>

            <div class="layui-form-item">
                <button type="submit" class="mt-4 btn btn-primary mb-3" lay-submit lay-filter="update">修改角色</button>
            </div>
        </form>
    </div>
</div>
{include file="common/bottom"}
<script>
    layui.use(function () {
        var form = layui.form
        var layer = layui.layer
        // 提交事件
        var objData = function () {
            let str = "{$data.auth}".split(',')
            let arr = []
            let obj = {
                'name': "{$data.name}"
            }
            str.forEach((e) => {
                if (e != '' && e != undefined) {
                    obj[`auth[${e}]`] = true
                }
            })
            if ("{$data.img_path}") {
                $('#close-upload').show()
                $('#ID-upload-demo-img').attr('src', "{$data.img_path}")
            }
            return obj
        }
        form.val('update', objData())
        form.on('submit(update)', function (data) {
            var field = data.field;
            field.id = "{$data.id}"
            $.post('/admin/role/doUpdate', field, (res) => {
                if (res.code == 200) {
                    layer.msg('修改成功', { icon: 6 })
                } else {
                    layer.msg('修改失败：' + res.message, { icon: 5 })
                }

            })
            return false; // 阻止默认 form 跳转
        });


    })
</script>